package com.example.starapp.luckfrag;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.animee.starapp.starfrag.StarInfo;
import com.example.starapp.R;
import com.example.starapp.utils.AssetsUtils;

import java.util.List;
public class LuckBaseAdapter extends BaseAdapter {
    Context context;
    List<StarInfo.StarinfoBean>mDatas;

    public LuckBaseAdapter(Context context, List<StarInfo.StarinfoBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();  //返回条目的数量
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);  //返回指定位置上对应的元素
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_luck_gv,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
//        获取指定位置的数据
        StarInfo.StarinfoBean bean = mDatas.get(position);
        holder.tv.setText(bean.getName()); //获取到星座名，设置到指定的TextView上
        String logoname = bean.getLogoname();
//        将图片所在的文件的整体路径名设置好
        String filename = "xzcontentlogo/"+logoname+".png";
        Bitmap bitmap = AssetsUtils.getBitmapByAssets(context, filename); //将图片转换成bitmap
//        将bitmap设置给imageview
        holder.iv.setImageBitmap(bitmap);
        return convertView;

    }
    /* 将每一个item对应的view当中的控件，都在此类当中声明和初始化*/
    class ViewHolder{
        ImageView iv;
        TextView tv;
        public ViewHolder(View view){
            iv = view.findViewById(R.id.item_luck_iv);
            tv = view.findViewById(R.id.item_luck_tv);
        }
    }
}